//
//  FavoriteGatewayViewController.m
//  Truffle
//
//  Created by m2sar on 15/03/10.
//  Copyright 2010 isrtream1-svn . All rights reserved.
//

#import "FavoriteGatewayViewController.h"
#import "DAOTruffle.h"
#import "Gateway.h"
#import "DatasTruffle.h"


@implementation FavoriteGatewayViewController


- (void)viewDidLoad {
	
    [super viewDidLoad];
	self.title = @"Favoris";
	
	dao = [DatasTruffle getDao];
	
	dbGatewayList = [[NSMutableArray alloc] init];
	
	dbGatewayList = [dao getGateways];
	
	// STATIC TEST
	//[dbGatewayList addObject:[[Gateway alloc] initGatewayRef:@"https://www.google.com.au/" :@"MyTestGatewat" :0]];
	
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
}


- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


#pragma mark Table view methods

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{	
	if (section == 0) {
		return @"Passerelle de Référence";
	} else {
		return @"Passerelles de Préférence";
	}
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	
	if (section == 0) {
		return 1;
	} else {
		return [dbGatewayList count]; // return size of fav gateway list
	}
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    UILabel * typeRef;
	
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    
	int nbviews = [[cell subviews] count];
	UIFont *displayFont = [UIFont boldSystemFontOfSize:14];
	
	[cell addSubview:[[UILabel alloc] initWithFrame:CGRectMake(60.0f, 13.0f, 150.0f, 20.0f)]];
	typeRef = [[cell subviews] objectAtIndex:nbviews];
	
	// UIButton pour ajouter en favoris
	UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
	[button setFrame:CGRectMake(15.0f, 8.0f, 30.0f, 30.0f)];
	
	// images d'étoiles
	UIImage *image=[UIImage imageNamed:@"favori.png"];
	UIImage *imageb=[UIImage imageNamed:@"favorib.png"];
	
	//NSString *cellText;
	typeRef.font = displayFont;
	int ro = indexPath.row;
	int sec = indexPath.section;
	
	if (sec == 0) {
		
		if ([[DatasTruffle getGateways] count] != 0) {
			
			Gateway *temp = [[DatasTruffle getGateways] objectAtIndex:0];
			
			if (![[temp name] isEqualToString:@"null"]){
			//if (temp.ref == 1) {
			
				typeRef.text = [[[DatasTruffle getGateways] objectAtIndex:0] name];
				cell.editing = NO;
		
				if(![dao isFavoriGateway:[[DatasTruffle getGateways] objectAtIndex:0]]) {
					
					[button setBackgroundImage:imageb forState:UIControlStateNormal];
					[button setBackgroundImage:image forState:UIControlStateHighlighted];
					button.tag=ro;
					[button addTarget:self action:@selector(ajoutFavoriGate:) forControlEvents:UIControlEventTouchUpInside];
				} else {
					[button setBackgroundImage:image forState:UIControlStateNormal];
					[button setBackgroundImage:image forState:UIControlStateHighlighted];
				}
				[cell addSubview:button];
			//}
			}
		}
		
	} else {
		
		if ([dbGatewayList count] != 0) {
		
			typeRef.text = [[dbGatewayList objectAtIndex:ro] name];
			cell.editing = YES;
		
			[button setBackgroundImage:image forState:UIControlStateNormal];
			[button setBackgroundImage:image forState:UIControlStateHighlighted];
			[cell addSubview:button];
		}
	}					
	
	cell.imageView.image = nil;
	cell.accessoryView = nil;
	
	return cell;
		
}

/* Sauvegarde un contact dans la BD */
- (void)ajoutFavoriGate:(id)sender {
	
	Gateway *refgateway = [[DatasTruffle getGateways] objectAtIndex:0];
	[refgateway setRef:0];
	
	if ( ([refgateway name] == nil) || ([refgateway name] == NULL)) {
		NSLog(@">>[FavoriteGateway -- ajoutFavoriGate()]Tried to add null gateway as favourite");
	} else {
		[dao addGateway:refgateway];
		dbGatewayList = [dao getGateways];
		// STATIC TEST
		//[dbGatewayList addObject:[[Gateway alloc] initGatewayRef:@"https://www.google.com.au/" :@"MyTestGatewat" :0]];
		[self.tableView reloadData];
	}
}

// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.

	if (indexPath.section == 0) {
		return NO;
	} else {
		return YES;
	}
	return NO;
}


// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
		[dao removeGateway:[dbGatewayList objectAtIndex:indexPath.row]];
		dbGatewayList = [dao getGateways];
		
		// STATIC TEST
		//[dbGatewayList addObject:[[Gateway alloc] initGatewayRef:@"https://www.google.com.au/" :@"MyTestGatewat" :0]];
		
		[self.tableView reloadData];
    }   
}

- (void)dealloc {
    [super dealloc];
}

@end

